package game.domain;

import game.configuration.Configuration;
import game.movement.Moveable;
import game.movement.MovementManager;
import game.shapes.BoundingBox;

import javax.vecmath.Point2d;
import java.awt.*;

public class Laser implements Moveable, Boundable {
	private static final double LASER_LENGTH = 50.0;
	private static final double SPEED = 4.0;
	private final Color colour = Color.RED;
	private boolean disabled = true;
	private double heading;
	private final MovementManager movementManager;
	private final Point2d position;
	private double speed;

	public Laser(Point2d position, double heading, int maxWidth, int maxHeight) {
		this.position = position;
		speed = SPEED;
		this.heading = heading;
		movementManager = new MovementManager(this, maxWidth, maxHeight, false);
	}

	public void animate() {
		if (!disabled) {
			movementManager.moveAndNeverWrap();
		}
	}

	public void disable() {
		disabled = true;
	}

	public void draw(Graphics2D graphics) {
		if (!disabled) {
			if (Configuration.getInstance().isDebugEnabled()) {
				graphics.setColor(Color.blue);
				graphics.draw(getBoundingBox().getRectangle());
			}

			graphics.setColor(colour);
			graphics.drawLine((int) position.x, (int) position.y, (int) (position.x + LASER_LENGTH * Math.cos(heading)), (int) (position.y + LASER_LENGTH * Math.sin(heading)));

			if (Configuration.getInstance().isDebugEnabled()) {
				graphics.setColor(Color.darkGray);
				graphics.drawString((int) position.x + "," + (int) position.y + ":" + heading, (int) position.x, (int) position.y + 20);
			}
		}
	}

	public BoundingBox getBoundingBox() {
		if (!disabled) {
			int x1 = (int) position.x;
			int y1 = (int) position.y;
			int x2 = (int) (position.x + LASER_LENGTH * Math.cos(heading));
			int y2 = (int) (position.y + LASER_LENGTH * Math.sin(heading));

			// int width = (int) Math.max(Math.abs(Math.cos(heading) *
			// LASER_LENGTH), 10);
			// int height = (int) Math.max(Math.abs(Math.sin(heading) *
			// LASER_LENGTH), 10);
			int x = Math.min(x1, x2);
			int y = Math.min(y1, y2);
			int width = Math.abs(x2 - x1);
			int height = Math.abs(y2 - y1);
			return new BoundingBox(new Rectangle(x, y, width, height));
		}
		return null;
	}

	public double getDirection() {
		// TODO Auto-generated method stub
		return 0;
	}

	public double getHeading() {
		return heading;
	}

	public Point2d getPosition() {
		return position;
	}

	public double getSpeed() {
		return speed;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void reset(Point2d position, double heading) {
		this.position.x = position.x;
		this.position.y = position.y;
		disabled = false;
		this.heading = heading;
	}

	public void setDirection(double direction) {
		// TODO Auto-generated method stub

	}

	public void setDisabled() {
		disabled = true;
	}

	public void setHeading(double heading) {
		this.heading = heading;
	}

	public void setSpeed(double speed) {
		this.speed = speed;
	}
}
